package com.liu.www;

public class Test_003 {

	/**
	 * 刘柱栋 2013-4-9
	 * 
	 * @param args
	 */
	public static void main(String[] args) {
		int[] a = {1,0,0,0,1,1,0};
		int[] n = {1,0,0,0,1,1,0,1,0};
		int result[] = getCounts(a, n);
		printArray(a);
		printArray(n);
		printArray(result);
	}

	private static void printArray(int[] n){
		for(int i : n){
			System.out.print(i + " ");
		}
		System.out.println();
	}
	
	private static int[] getCounts(int[] n, int[] a) {
		int n_size = n.length;
		int a_size = a.length;
		int result_size = 0;
		int size = 0;
		boolean flag = false;
		if (n_size >= a_size) {
			size = a_size;
			flag = true;
			result_size = n_size + 1;
		} else {
			size = n_size;
			result_size = a_size + 1;
		}

		int[] result = new int[result_size];
		int temp = 0;

		for (int i = 0; i < size; i++) {
			int[] sum = getSum(a[i], n[i], temp);
			result[i] = sum[0];
			temp = sum[1];
		}

		for (int i = size; i < result_size - 1; i++) {
			int[] sum;
			if (flag) {
				sum = getSum(0, n[i], temp);
			} else {
				sum = getSum(a[i], 0, temp);
			}
			result[i] = sum[0];
			temp = sum[1];
		}
		
		result[result_size -1] = temp;
		return result;
	}

	private static int[] getSum(int a, int n, int temp) {
		int[] sums = new int[2];

		if (a == 0 && n == 0) {
			sums[0] = temp;
		} else if (a == 1 && n == 1) {
			sums[0] = temp;
			sums[1] = 1;
		} else if (a == 1 || n == 1) {
			if (temp == 1) {
				sums[0] = 0;
				sums[1] = 1;
			} else {
				sums[0] = 1;
			}
		}
		return sums;
	}
}
